package light.mvc.controller.wxn;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import light.mvc.controller.base.BaseController;
import light.mvc.pageModel.base.Grid;
import light.mvc.pageModel.base.Json;
import light.mvc.pageModel.base.PageFilter;
import light.mvc.pageModel.wxn.WeiMenuVO;
import light.mvc.pageModel.wxn.WeiTagsVO;
import light.mvc.service.wxn.AccessTokenServiceI;
import light.mvc.service.wxn.WeiTagsServiceI;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/weiTags")
public class WeiTagsController extends BaseController {

	@Autowired
	private WeiTagsServiceI weiTagsService;
	
	@Autowired
	private AccessTokenServiceI accessTokenService;

	@RequestMapping("/manager")
	public String manager() {
		return "/wxn/weiTags";
	}
	
	@RequestMapping("/treeGrid")
	@ResponseBody
	public List<WeiTagsVO> treeGrid() {
		return weiTagsService.treeGrid();
	}

	@RequestMapping("/dataGrid")
	@ResponseBody
	public Grid dataGrid(WeiTagsVO weiTags, PageFilter ph) {
		Grid grid = new Grid();
		grid.setRows(weiTagsService.dataGrid(weiTags, ph));
		grid.setTotal(weiTagsService.count(weiTags, ph));
		return grid;
	}

	@RequestMapping("/viewPage")
	public String viewPage(HttpServletRequest request, Long id) {
		WeiTagsVO r = weiTagsService.get(id);
		request.setAttribute("weiTags", r);
		return "/wxn/weiTagsView";
	}

	@RequestMapping("/addPage")
	public String addPage() {
		return "/wxn/weiTagsAdd";
	}

	@RequestMapping("/add")
	@ResponseBody
	public Json add(WeiTagsVO weiTags) {
		Json j = new Json();
		try {
			weiTagsService.add(weiTags);
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Json delete(Long id) {
		Json j = new Json();
		try {
			weiTagsService.delete(id);
			j.setMsg("删除成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/get")
	@ResponseBody
	public WeiTagsVO get(Long id) {
		return weiTagsService.get(id);
	}

	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request, Long id) {
		WeiTagsVO r = weiTagsService.get(id);
		request.setAttribute("weiTags", r);
		return "/wxn/weiTagsEdit";
	}

	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(WeiTagsVO weiTags) {
		Json j = new Json();
		try {
			weiTagsService.edit(weiTags);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/tagsGet")
	@ResponseBody
	public Json tagsGet(String appid) {
		return accessTokenService.tagsGet(appid);
	}
}